<?php
declare (strict_types=1);

namespace app\course\model\base;

use app\common\model\BaseModel;
use think\model\relation\BelongsTo;

/**
 * 学习班模型
 */
class StudyClassLink extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'course_study_class_link';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 不需要更新时间
     *
     * @var boolean
     */
    protected $updateTime = false;

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'class_id'    => 'int',
        'course_id'   => 'int',
        'user_id'     => 'int',
        'create_time' => 'int',
    ];

    /**
     * scopeTraining
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/08/07
     *
     * @param $query
     */
    public function scopeTraining($query)
    {
        $query->where('training_start_time', '<', time())->where('training_end_time', '>', time());
    }

    /**
     * classInfo
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/05/18
     */
    public function classTrainingTime(): BelongsTo
    {
        return $this->belongsTo(StudyClass::class, 'class_id')->bind(['training_start_time', 'training_end_time']);
    }

    public function classInfo(): BelongsTo
    {
        return $this->belongsTo(StudyClass::class, 'class_id');
    }
}
